package sn.hn.demo.unit;

import java.util.Random;

import sn.hn.demo.MainActivity;
import sn.hn.demo.image.ImageData;
import sn.hn.demo.sprite.MySprite;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Fish extends Unit {
	public final int FLIP_VERTICAL = 1;
	public final int FLIP_HORIZONTAL = 2;
	Random r = new Random();
	public Bitmap mFish;
	public Bitmap mFishUp;
	public MySprite mSprite;
	public boolean isLeft;
	public boolean isUp;
	public int speed;
	public boolean isCatched;
	public int score;
	public boolean die;
	public static int totalFrame = 8;

	public Fish(Context context) {
		super(context);
		isUp = false;
		isCatched = false;
		isLeft = r.nextBoolean();
		speed = r.nextInt(2) + 1;
		die = false;
		getBitmapFish();
		if (isLeft) {
			x = MainActivity.SCREENWIDTH + r.nextInt(10000)%100;
			y = 3*MainActivity.SCREENHEIGHT/8 + this.mFish.getHeight() + r.nextInt(10000)%60;
		} else {
			x = -this.mFish.getWidth() - r.nextInt(10000)%100;
			y = 3*MainActivity.SCREENHEIGHT/8 + this.mFish.getHeight() + r.nextInt(10000)%60;
		}
		mSprite = new MySprite(x, y);
		mSprite.init(mFish, mFish.getWidth()/totalFrame, mFish.getHeight(), totalFrame, totalFrame);
	}

	public Bitmap flip(Bitmap src, int type) {
		// create new matrix for transformation
		Matrix matrix = new Matrix();
		// if vertical
		if(type == FLIP_VERTICAL) {
			// y = y * -1
			matrix.preScale(1.0f, -1.0f);
		}
		// if horizonal
		else if(type == FLIP_HORIZONTAL) {
			// x = x * -1
			matrix.preScale(-1.0f, 1.0f);
		// unknown type
		} else {
			return src;
		}
		// return transformed image
		return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
	}
	
	public void getBitmapFish()
	{
		Random r = new Random();
		switch (r.nextInt(100)%6) {
		case 0:
			mFish = ImageData.mCa1;
			mFishUp = ImageData.mCa1up;
			score = 20;
			break;
		case 1:
			mFish = ImageData.mCa2;
			mFishUp = ImageData.mCa2up;
			score = 30;
			break;
		case 2:
			mFish = ImageData.mCa3;
			mFishUp = ImageData.mCa3up;
			score = 40;
			break;
		case 3:
			mFish = ImageData.mCa4;
			mFishUp = ImageData.mCa4up;
			score = 50;
			break;
		case 4:
			mFish = ImageData.mCa5;
			mFishUp = ImageData.mCa5up;
			score = 70;
			break;
		case 5:
			mFish = ImageData.mCa6;
			mFishUp = ImageData.mCa6up;
			score = 80;
			break;
		default:
			mFish = ImageData.mCa1;
			mFishUp = ImageData.mCa1up;
			break;
		}
		if(!this.isLeft)
			mFish = flip(mFish, FLIP_HORIZONTAL);
		
	}
	
	public void moveFish() {
		if (!isUp) {
			if (this.isLeft) {
				if (this.x > -150)
					this.x -= this.speed;
				else
				{
					die = true;
				}
			} else {
				if (this.x < MainActivity.SCREENWIDTH + this.mFish.getWidth()/2)
					this.x += this.speed;
				else
				{
					die = true;
				}
			}
			mSprite.xPos = this.x;
		}
	}
}
